//
//  HSetUpViewController.m
//  Router
//
//  Created by Zhen Huang on 2018/6/26.
//  Copyright © 2018年 James. All rights reserved.
//

#import "HSetUpViewController.h"
#import "HPlugViewController.h"
#import "HFeedBackViewController.h"
#import "RegisterAndLoginViewController.h"
#import "WCustomNavigationController.h"
#import "ChangeIphoneAndPassWordViewController.h"
#import "HChangeMobileViewController.h"
#import "AppDelegate.h"
@interface HSetUpViewController ()
<UITableViewDelegate,
UITableViewDataSource>
@property (nonatomic, strong) UITableView *tabelview;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSArray *SectionArray;
@property (nonatomic, strong) NSArray *SectionArray1;
@end

@implementation HSetUpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBackGroundColor;
    [self.view addSubview:self.tabelview];
    self.SectionArray = @[@"修改密码",@"更换手机号",@"意见反馈"];
    self.SectionArray1 = @[@"插件安装向导",@"官方QQ群"];
}

- (UITableView *)tabelview {
    if (!_tabelview) {
        NSString *deviceType = [UIDevice currentDevice].model;
        _tabelview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - NavBarHeight) style:UITableViewStyleGrouped];
        if ([deviceType isEqualToString:@"iPad"]) {
            _tabelview.top = NavBarHeight;
        }
        _tabelview.delegate = self;
        _tabelview.dataSource = self;
        _tabelview.backgroundColor = kBackGroundColor;
        _tabelview.separatorColor = [UIColor colorWithCGColor:[kStrokeBackGroundColor CGColor]];
        [_tabelview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CellID"];
        _tabelview.tableFooterView = [[UIView alloc]init];
    }
    return _tabelview;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID" forIndexPath:indexPath];
    cell.backgroundColor = kWidgetBackGroundColor;
    if (indexPath.section == 0) {
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 50, 50)];
        image.image = [UIImage imageNamed:@"touxiang"];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(image.right + 10, 15, 100,30)];
        label.centerY = image.centerY;
        label.text = [[TMCache sharedCache]objectForKey:@"UserName"]?:@"";
        label.font = [UIFont fontWithName:PingFangSCRegular size:15];
        label.textColor = kTitleBackGroundColor;
        cell.separatorInset = UIEdgeInsetsMake(0, ScreenWidth, 0, 0); 
        [cell addSubview:image];
        [cell addSubview:label];
    }else if (indexPath.section == 1) {
        cell.textLabel.text = self.SectionArray[indexPath.row];
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth - 30, 14, 16, 16)];
        image.image = [UIImage imageNamed:@"Go"];
        [cell addSubview:image];
    }else if(indexPath.section == 2) {
        cell.textLabel.text = self.SectionArray1[indexPath.row];
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth - 30, 14, 16, 16)];
        image.image = [UIImage imageNamed:@"Go"];
        [cell addSubview:image];
    }
    cell.textLabel.textColor = kTitleBackGroundColor;
    cell.textLabel.font = [UIFont fontWithName:PingFangSCRegular size:13];
    cell.preservesSuperviewLayoutMargins = NO;
    cell.separatorInset = UIEdgeInsetsZero;
    cell.layoutMargins = UIEdgeInsetsZero;
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = kBackGroundColor;
    cell.selectedBackgroundView = view;
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else if (section == 1) {
        return 3;
    }else if (section == 2) {
        return 2;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 2) {
        return 150;
    }else {
        return 15;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 60;
    }
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 2) {
        UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
        UIButton *SubmitButton = [UIButton buttonWithType:UIButtonTypeCustom];
        SubmitButton.frame = CGRectMake(30, 90, ScreenWidth - 60, 50);
        [SubmitButton setTitle:@"切换账号" forState:UIControlStateNormal];
        [SubmitButton setTitleColor:kNavBlueColor forState:UIControlStateNormal];
        SubmitButton.backgroundColor = kBackGroundColor;
        SubmitButton.layer.masksToBounds = YES;
        SubmitButton.layer.cornerRadius = 25;
        SubmitButton.layer.borderColor = [kNavBlueColor CGColor];
        SubmitButton.layer.borderWidth = 1.0f;
        [[SubmitButton rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x){
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                           message:@"是否注销当前用户?"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {
                                                                      [[TMCache sharedCache]removeObjectForKey:@"Token"];
                                                                      [[TMCache sharedCache]removeObjectForKey:@"UserName"];
                                                                      AppDelegate *app = (AppDelegate *)[[UIApplication  sharedApplication] delegate];
                                                                      [app rootViewToTabBar];
                                                                  }];
            UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                                 handler:^(UIAlertAction * action) {
                                                                 }];
            [alert addAction:cancelAction];
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }];
        [footView addSubview:SubmitButton];
        return footView;
    }else {
        return nil;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section == 2 && indexPath.row == 0) {
        HPlugViewController *HPlugVC = [[HPlugViewController alloc]init];
        HPlugVC.title = @"插件安装向导";
        [self.navigationController pushViewController:HPlugVC animated:YES];
    }
    if (indexPath.section == 1 && indexPath.row == 2) {
        HFeedBackViewController *HFeedBackVC = [[HFeedBackViewController alloc]init];
        HFeedBackVC.title = @"用户反馈";
        [self.navigationController pushViewController:HFeedBackVC animated:YES];
    }
    if (indexPath.section == 2 && indexPath.row == 1) {
        [self joinGroup];
    }
    if (indexPath.section == 1 && indexPath.row == 0) {
        ChangeIphoneAndPassWordViewController *VC = [[ChangeIphoneAndPassWordViewController alloc]init];
        VC.title = @"修改密码";
        [self.navigationController pushViewController:VC animated:YES];
    }
    if (indexPath.section == 1 && indexPath.row == 1) {
        HChangeMobileViewController *VC = [[HChangeMobileViewController alloc]init];
        VC.title = @"更换手机号";
        [self.navigationController pushViewController:VC animated:YES];
    }
}
-(void)joinGroup {
    NSString *url = F(@"%@/qq_group", BaseUrl);
    [self receivePOSTWithTokenURLString:url method:@"GET" body:nil Token:nil Blocks:^(NSDictionary *tempData, NSInteger state) {
        if (tempData) {
            [self joinGroup:[tempData valueForKey:@"qq"][0] key:[tempData valueForKey:@"iphone"][0]];
        }
    }];
}
//一键加QQ群
- (BOOL)joinGroup:(NSString *)groupUin key:(NSString *)key{
    NSString *urlStr = [NSString stringWithFormat:@"mqqapi://card/show_pslcard?src_type=internal&version=1&uin=%@&key=%@&card_type=group&source=external", groupUin,key];
    NSURL *url = [NSURL URLWithString:urlStr];
    if([[UIApplication sharedApplication] canOpenURL:url]){
        [[UIApplication sharedApplication] openURL:url];
        return YES;
    }else {
        [HCommon popupErrorMessage:@"打开QQ失败,请重试"];
        return NO;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
